package qldc.pm.tiensu.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qldc.pm.qtht.dmdc.utils.DiaChiUtils;
import qldc.pm.tiensu.NoSuchKetQuaTrichLucException;
import qldc.pm.tiensu.dto.DoiTuongTrichLucDTO;
import qldc.pm.tiensu.model.DoiTuongTienSu;
import qldc.pm.tiensu.model.KetQuaTrichLuc;
import qldc.pm.tiensu.model.YeuCauTrichLuc;
import qldc.pm.tiensu.model.impl.KetQuaTrichLucImpl;
import qldc.pm.tiensu.service.base.KetQuaTrichLucLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;

/**
 * The implementation of the ket qua trich luc local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link qldc.pm.tiensu.service.KetQuaTrichLucLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tiensu.service.base.KetQuaTrichLucLocalServiceBaseImpl
 * @see qldc.pm.tiensu.service.KetQuaTrichLucLocalServiceUtil
 */
public class KetQuaTrichLucLocalServiceImpl
    extends KetQuaTrichLucLocalServiceBaseImpl {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never reference this interface directly. Always use {@link qldc.pm.tiensu.service.KetQuaTrichLucLocalServiceUtil} to access the ket qua trich luc local service.
     */
	private static Log _log = LogFactoryUtil.getLog(KetQuaTrichLucLocalServiceBaseImpl.class);

	public KetQuaTrichLuc addKetQuaTrichLuc(User user, String maKetQuaTrichLuc, 
			long yeuCauTrichLucId,
			String kinhGui,
			String tinhTrangAnTich,
			String thongTinAnTich,
			String toiDanh,
			String hinhPhatChinh, String hinhPhatBoSung,
	    	String trinhTrangThiHanhAn,
	    	String nguoiTraCuuHoSo,
	    	Date ngayTraCuu, int ketQua
	    	) throws SystemException {
    	long ketQuaTrichLucId = counterLocalService.increment(KetQuaTrichLuc.class.getName());
    	KetQuaTrichLuc ketQuaTrichLuc = createKetQuaTrichLuc(ketQuaTrichLucId);
	
    	ketQuaTrichLuc.setYeuCauTrichLucId(yeuCauTrichLucId);
    	ketQuaTrichLuc.setKinhGui(kinhGui);
    	ketQuaTrichLuc.setMaKetQuaTrichLuc(maKetQuaTrichLuc);
    	ketQuaTrichLuc.setTinhTrangAnTich(tinhTrangAnTich);
    	ketQuaTrichLuc.setThongTinAnTich(thongTinAnTich);
    	ketQuaTrichLuc.setToiDanh(toiDanh);
    	ketQuaTrichLuc.setHinhPhatChinh(hinhPhatChinh);
    	ketQuaTrichLuc.setHinhPhatBoSung(hinhPhatBoSung);
    	ketQuaTrichLuc.setTrinhTrangThiHanhAn(trinhTrangThiHanhAn);
    	ketQuaTrichLuc.setNguoiTraCuuHoSo(nguoiTraCuuHoSo);
    	ketQuaTrichLuc.setNgayTraCuu(ngayTraCuu);
    	ketQuaTrichLuc.setKetQua(ketQua);
    	

    	if (yeuCauTrichLucId > 0){
    		trichLucXong(yeuCauTrichLucId);
    	}

    	return addKetQuaTrichLuc(ketQuaTrichLuc);
    }

    public List<KetQuaTrichLuc> findAll() {
    	List<KetQuaTrichLuc> KetQuaTrichLucs = new ArrayList<KetQuaTrichLuc>();

    	try {
			KetQuaTrichLucs = ketQuaTrichLucPersistence.findAll();
		} catch (SystemException e) {
			_log.warn(e);
		}

    	return KetQuaTrichLucs;
    }

    public int countAll() {
    	int count = 0;

    	try {
			count = ketQuaTrichLucPersistence.countAll();
		} catch (SystemException e) {
			_log.warn(e);
		}

    	return count;
    }
    public int countByDS_TrichLuc(
			long tinhId,long quanHuyenId,long phuongXaId, Date tuNgayTrichLuc, Date denNgayTrichLuc,String coQuanYeuCauTrichLuc,String nguoiThucHienTrichLuc) throws SystemException
    	{
    		return ketQuaTrichLucFinder.countDS_TrichLuc(tinhId, quanHuyenId, phuongXaId, tuNgayTrichLuc, denNgayTrichLuc, coQuanYeuCauTrichLuc, nguoiThucHienTrichLuc);
    	}
    public List<DoiTuongTrichLucDTO> findByDS_TrichLuc(
			long tinhId,long quanHuyenId,long phuongXaId, Date tuNgayTrichLuc, Date denNgayTrichLuc,String coQuanYeuCauTrichLuc,String nguoiThucHienTrichLuc){
				

    	try {
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			List<Object[]> objects = ketQuaTrichLucFinder.findByDS_TrichLuc(tinhId, quanHuyenId, phuongXaId, tuNgayTrichLuc, denNgayTrichLuc, coQuanYeuCauTrichLuc, nguoiThucHienTrichLuc);
					 
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DoiTuongTrichLucDTO> dsDoiTuongTrichLuc = new ArrayList<DoiTuongTrichLucDTO>();

				Object[] objDoiTuong = null;
				DoiTuongTrichLucDTO doiTuong = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					doiTuong = new DoiTuongTrichLucDTO();
					doiTuong.setCoQuanYeuCauTrichLuc(objDoiTuong[0] != null ? objDoiTuong[0].toString() : "");
					doiTuong.setHoVaTen(objDoiTuong[1] != null ? objDoiTuong[1].toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							Long.parseLong(objDoiTuong[4].toString()),
							objDoiTuong[5].toString());
					doiTuong.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru: "");
	

					if (objDoiTuong[6] != null){
						Date ngayTrichLuc = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[6].toString());
						doiTuong.setNgayTrichLuc(ngayTrichLuc);
					}
					doiTuong.setNguoiTrichLuc(objDoiTuong[7] != null ? objDoiTuong[7].toString() : "");							
					doiTuong.setSoDinhDanhCaNhan(objDoiTuong[8] != null ? objDoiTuong[8].toString() : "");						
					doiTuong.setSoCMND(objDoiTuong[9] != null ? objDoiTuong[9].toString() : "");							
					if (objDoiTuong[10] != null){
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[10].toString());
						doiTuong.setNgaySinh(ngaySinh);
					}
					doiTuong.setKetQuaTrichLuc(objDoiTuong[11] != null ? Integer.parseInt(objDoiTuong[11].toString()) : 0);					

					dsDoiTuongTrichLuc.add(doiTuong);

				}

				return dsDoiTuongTrichLuc;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;

    	
    }

    public KetQuaTrichLuc findByKetQuaTrichLucId(long ketQuaTrichLucId) {
    	KetQuaTrichLuc KetQuaTrichLuc = null;

    	try {
			KetQuaTrichLuc = ketQuaTrichLucPersistence.findByPrimaryKey(ketQuaTrichLucId);
		} catch (NoSuchKetQuaTrichLucException e) {
			_log.warn(e.getMessage());
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

    	return KetQuaTrichLuc;
    }
    public KetQuaTrichLuc findByYeuCauTrichLucId(long yeuCauTrichLucId) {
    	KetQuaTrichLuc ketQuaTrichLuc = null;

    	try {
    		ketQuaTrichLuc = ketQuaTrichLucPersistence.findByYeuCauTrichLucId(yeuCauTrichLucId);
		} catch (NoSuchKetQuaTrichLucException e) {
			_log.warn(e.getMessage());
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

    	return ketQuaTrichLuc;
    }
	public KetQuaTrichLuc updateKetQuaTrichLuc(long ketQuaTrichLucId, User user, String maKetQuaTrichLuc, 
			
			long yeuCauTrichLucId,
			String kinhGui,
			String tinhTrangAnTich,
			String thongTinAnTich,
			String toiDanh,
			String hinhPhatChinh, String hinhPhatBoSung,
	    	String trinhTrangThiHanhAn,
	    	String nguoiTraCuuHoSo,
	    	Date ngayTraCuu, int ketQua
	    	) throws SystemException, PortalException {
    	KetQuaTrichLuc ketQuaTrichLuc = getKetQuaTrichLuc(ketQuaTrichLucId);
    	
    	ketQuaTrichLuc.setYeuCauTrichLucId(yeuCauTrichLucId);
    	ketQuaTrichLuc.setKinhGui(kinhGui);
    	ketQuaTrichLuc.setMaKetQuaTrichLuc(maKetQuaTrichLuc);
    	ketQuaTrichLuc.setTinhTrangAnTich(tinhTrangAnTich);
    	ketQuaTrichLuc.setThongTinAnTich(thongTinAnTich);
    	ketQuaTrichLuc.setToiDanh(toiDanh);
    	ketQuaTrichLuc.setHinhPhatChinh(hinhPhatChinh);
    	ketQuaTrichLuc.setHinhPhatBoSung(hinhPhatBoSung);
    	ketQuaTrichLuc.setTrinhTrangThiHanhAn(trinhTrangThiHanhAn);
    	ketQuaTrichLuc.setNguoiTraCuuHoSo(nguoiTraCuuHoSo);
    	ketQuaTrichLuc.setNgayTraCuu(ngayTraCuu);
    	ketQuaTrichLuc.setKetQua(ketQua);
    	
    	if (yeuCauTrichLucId > 0){
    		trichLucXong(yeuCauTrichLucId);
    	}

    	return updateKetQuaTrichLuc(ketQuaTrichLuc);
    }
	public void trichLucXong(long yeuCauTrichLucId) throws SystemException{
		YeuCauTrichLuc yeuCau = yeuCauTrichLucPersistence.fetchByPrimaryKey(yeuCauTrichLucId);
		if (Validator.isNotNull(yeuCau)){
			yeuCau.setDaTrichLuc(true);
			yeuCauTrichLucPersistence.update(yeuCau);
		}
	}

}
